﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class city_s_review : System.Web.UI.Page
{
    int no;
    ctpDataContext db;

    public bool is_manager;
              

    protected void Page_Load(object sender, EventArgs e)
    {
        no = int.Parse(Request["no"]);
        ctp_lib.check_login(this.Page, "city_s_review.aspx?no=" + no.ToString());
        db = new ctpDataContext();
        int userno = (int)Session["userno"];

        if (!this.IsPostBack)
        {
            ct_s_profile cts = db.ct_s_profiles.Single(p => p.valid == true && p.no == no );

            this.ViewState["ct_no"] = cts.ct_no;

            //check is manager
            if (db.honor_citizens.Count(p => p.ct_no == cts.ct_no && p.user_no == userno) != 0)
            {
                is_manager = true;

            }
            else if (db.ct_profiles.Count(p => p.no == cts.ct_no && p.manager_user_no == userno) != 0)
            {
                is_manager = true;

            }
            else
            {
                is_manager = false;
            }

            if (is_manager)
            {

                if (db.mayor_recommends.Count(p => p.type == 1 && p.user_no == userno && p.ct_s_no == no) != 0)
                {
                    this.Label_travel_title.Visible = false;
                    this.TextBox_travel_coment.Visible = false;
                    this.Button_travel_ok.Visible = false;


                }
                else
                {
                    this.Label_trave_already.Visible = false;
                    this.LinkButton_travel_remove.Visible = false;

                }

                if (db.mayor_recommends.Count(p => p.type == 2 && p.user_no == (int)Session["userno"] && p.ct_s_no == no) != 0)
                {
                    this.Label_local_title.Visible = false;
                    this.TextBox_local_coment.Visible = false;
                    this.Button_local_ok.Visible = false;
                }
                else
                {
                    this.Label_local_already.Visible = false;
                    this.LinkButton_local_remove0.Visible = false;

                }

            }

            this.HyperLink3.Text = cts.title;
            this.HyperLink3.NavigateUrl = "city_s.aspx?no=" + no.ToString().Trim();
            this.HyperLink2.Text = cts.ct_profile.title.Trim();
            this.HyperLink2.NavigateUrl = "city.aspx?no=" + cts.ct_no;
            this.CheckBox1.Text = "I live in this city ("+cts.ct_profile.title.Trim()+").";
            this.CheckBox2.Text = "I have been to this item ("+cts.title.Trim()+").";


            this.Label3.Text = "Travel Guid page";
            this.Label4.Text =  "For Locals page";

            this.Label6.Text ="Add this to "+ cts.ct_profile.title.Trim();
            this.Label7.Text = "Add this to " + cts.ct_profile.title.Trim();


            this.RadioButtonList1.Items.Add(new ListItem("People first time to "+cts.ct_profile.title.Trim(),"1"));
            this.RadioButtonList1.Items.Add(new ListItem("People who live in " + cts.ct_profile.title.Trim(),"2"));

            int reviewed = db.ct_s_reviews.Count(p => p.create_user_no == (int)Session["userno"] && p.ct_s_no == no);

            if (reviewed == 1)
            {
                ct_s_review r = db.ct_s_reviews.Single(p => p.create_user_no == (int)Session["userno"] && p.ct_s_no == no);

                this.DropDownList1.SelectedValue = r.ranking.ToString();
                this.TextBox1.Text = r.comment.Trim();
                this.CheckBox1.Checked = r.is_local;
                this.CheckBox2.Checked = r.been_there;

                if (r.recommand_to != -1) this.RadioButtonList1.SelectedValue = r.recommand_to.ToString();


            }


        }

        //if (int.Parse(this.DropDownList1.SelectedValue) >= 6)
        //{
        //    this.Label2.Enabled = true;
        //    this.RadioButtonList1.Enabled = true;
        //}
        //else
        //{
        //    this.Label2.Enabled = false;
        //    this.RadioButtonList1.Enabled = false;


        //}

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        int reviewed = db.ct_s_reviews.Count(p => p.create_user_no == (int)Session["userno"] && p.ct_s_no == no);
        ct_s_review r;

        if (reviewed != 0)
        {
            r = db.ct_s_reviews.Single(p => p.create_user_no == (int)Session["userno"] && p.ct_s_no == no);
        }
        else
        {
            r = new ct_s_review();
        }

        r.ranking = int.Parse(this.DropDownList1.SelectedValue);
        r.create_user_no = (int)Session["userno"];
        r.ct_s_no = no;
        r.been_there = this.CheckBox2.Checked;
        r.is_local = this.CheckBox1.Checked;
        r.addtime = DateTime.Now;

        r.ct_no = (int)this.ViewState["ct_no"];

        int recommand = -1;
        if (this.RadioButtonList1.Enabled)
        {
            if (this.RadioButtonList1.SelectedItem != null)
                recommand = int.Parse(this.RadioButtonList1.SelectedValue);
        }
        r.recommand_to = recommand;

        if (this.CheckBox1.Checked)
        {
            user myuser = db.users.Single(p => p.no == (int)Session["userno"]);
            myuser.ct_no = (int)this.ViewState["ct_no"];
        }


        r.comment = this.TextBox1.Text;



        if (reviewed == 0)
        {
            db.ct_s_reviews.InsertOnSubmit(r);

        }

        db.SubmitChanges();

        var r0 = from p in db.ct_s_reviews
                 where p.ct_s_no == no
                 select p.ranking;

        var r1 = from p in db.ct_s_reviews
                 where p.is_local == true && p.ct_s_no == no
                 select p.ranking;

        var r2 = from p in db.ct_s_reviews
                 where p.been_there == true && p.ct_s_no == no
                 select p.ranking;

        Double ranking = 0;
        if (r0.Count() != 0)
        {
            ranking = (double)r0.Average();
            ranking = ((int)(ranking * 10)) / 10.0;
        }

        Double ranking_local = 0;
        if (r1.Count() != 0)
        {
            ranking_local = (double)r1.Average();
            ranking_local = ((int)(ranking_local * 10)) / 10.0;
        }

        Double ranking_been_there = 0;
        if (r2.Count() != 0)
        {
            ranking_been_there = (double)r2.Average();
            ranking_been_there = ((int)(ranking_been_there * 10)) / 10.0;
        }

        int ranking_amount = db.ct_s_reviews.Count(p => p.ct_s_no == no);
        int recommend_first = db.ct_s_reviews.Count(p => (p.recommand_to == 0 || p.recommand_to == 1) && p.ct_s_no == no);
        int recommend_local = db.ct_s_reviews.Count(p => (p.recommand_to == 0 || p.recommand_to == 2) && p.ct_s_no == no);
        int recommend_everyone = db.ct_s_reviews.Count(p => p.recommand_to != -1 && p.ct_s_no == no);



        var cts = from p in db.ct_s_profiles
                  where p.no == no && p.valid == true
                  select p;

        foreach (ct_s_profile c in cts)
        {
            c.ranking = ranking;
            c.ranking_been_there = ranking_been_there;
            c.ranking_local = ranking_local;
            c.rank_amount = ranking_amount;
            c.recommend_to_everyone = recommend_everyone;
            c.recommend_to_first = recommend_first;
            c.recommend_to_locals = recommend_local;

        }

        db.SubmitChanges();

        Response.Redirect("city_s.aspx?no=" + no.ToString());


    }

    void recommend(string text,int type)
    {

        int user_lv = 0;
        int ct_no=(int)this.ViewState["ct_no"];
        int userno=(int)Session["userno"];

        if (db.honor_citizens.Count(p => p.ct_no == ct_no && p.user_no == userno) != 0)
        {
            user_lv = 1;

        }
        else if (db.ct_profiles.Count(p => p.no == ct_no && p.manager_user_no == userno) != 0)
        {
            user_lv = 2;

        }
        else
        {
            return;
        }


        mayor_recommend my_recomm = new mayor_recommend();
        my_recomm.addtime = DateTime.Now;
        my_recomm.ct_no = ct_no;
        my_recomm.ct_s_no = no;
        my_recomm.intro = text;
        my_recomm.user_level = user_lv;
        my_recomm.user_no = userno;
        my_recomm.type = type;

        db.mayor_recommends.InsertOnSubmit(my_recomm);
        db.SubmitChanges();

        if (type == 1)
        {
            Response.Redirect("city_travel.aspx?no=" + ct_no);
        }
        else
        {
            Response.Redirect("city_local.aspx?no=" + ct_no);
        }

    }

    void remove_this(int type)
    {
        int ct_no = (int)this.ViewState["ct_no"];
        int userno = (int)Session["userno"];

        mayor_recommend my_recomm = db.mayor_recommends.Single(p => p.user_no == userno && p.ct_s_no == no && p.type == type);

        db.mayor_recommends.DeleteOnSubmit(my_recomm);
        db.SubmitChanges();

        if (type == 1)
        {
            Response.Redirect("city_travel.aspx?no=" + ct_no);
        }
        else
        {
            Response.Redirect("city_local.aspx?no=" + ct_no);
        }


    }

    protected void Button_travel_ok_Click(object sender, EventArgs e)
    {
        this.recommend(this.TextBox_travel_coment.Text,1);

    }
    protected void Button_local_ok_Click(object sender, EventArgs e)
    {
        this.recommend(this.TextBox_local_coment.Text,2);
    }
    protected void LinkButton_travel_remove_Click(object sender, EventArgs e)
    {
        this.remove_this(1);

    }
    protected void LinkButton_local_remove0_Click(object sender, EventArgs e)
    {
        this.remove_this(2);
    }
}
